/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mrroman.linksender.gui.actions;

import com.mrroman.linksender.Configuration;
import com.mrroman.linksender.filesender.ServerManager;
import com.mrroman.linksender.filesender.server.EncodingTools;
import com.mrroman.linksender.filesender.sendables.Sendable;
import com.mrroman.linksender.ioc.*;
import com.mrroman.linksender.sender.Message;
import com.mrroman.linksender.sender.MessageEvent;
import com.mrroman.linksender.sender.MessagePoint;
import com.mrroman.linksender.sender.PrivateMessagePoint;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;

/**
 *
 * @author gorladam
 */
@Name("actions.ShareDirAction")
public class ShareDirAction extends AbstractAction {

    @Locales
    private ResourceBundle messages;
    @In
    private MessagePoint msgPoint;
    @In
    private PrivateMessagePoint privateMessagePoint;
    @In
    private Configuration config;
    @In
    private ServerManager manager;

    @Init
    public void init() {
        putValue(NAME, messages.getString("share_dir"));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle(messages.getString("share_dir"));
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setMultiSelectionEnabled(false);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            Sendable shared = null;
            String msgStr = null;
            shared = manager.shareFile(file, ServerManager.ALL_IP);
            String sharedName = EncodingTools.urlEncodeUTF(shared.toString());
            String address = "http://<sender>:" + manager.getPort() + "/" + sharedName;
            msgStr = messages.getString("sharing_file") + " " + address;
            Message msg = new Message(config.getNick(), msgStr);
            privateMessagePoint.systemMessage(MessageEvent.EventType.INFO, msgStr.replace("<sender>", "127.0.0.1"));
            msgPoint.sendMessage(msg);
        }
    }
}
